package com.youlu.campus.common.utils;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class WebAuthUtils {

    public static final String delimiter = "^^";
    public static final String signKey = "(#IKJ&HGH^666u";

    public static String signToken(String openId) {
        log.info(":>>> 处理签名:{}", openId);
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(signKey.getBytes(), "HmacSHA256"));
            String s = new String(Hex.encode(mac.doFinal(openId.getBytes())));
            return openId + delimiter + s;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }
}
